﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DispatchAssist.Weather.Helper
{
    /// <summary>
    /// 循环队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks>
    /// 用于图片动态加载和播放
    /// 线程1中不断将下载完成的图片压入队列最后面
    /// 线程2不断循环播放队列中的图片
    /// </remarks>
    class CircularQueue<T>
    {
        public int Count { get { return queueList.Count; } }
        /// <summary>
        /// 对列当前的对象
        /// </summary>
        public T Current { get; private set; }

        IList<T> queueList = new List<T>();
        private int index = -1;
        private static object lockObj = new object();
        /// <summary>
        /// 入队列
        /// </summary>
        /// <param name="t"></param>
        public void EnQueue(T t)
        {
            lock (lockObj)
            {
                queueList.Add(t);
            }
        }
        /// <summary>
        /// 出队列
        /// </summary>
        /// <returns></returns>
        public T DeQueue()
        {
            lock (lockObj)
            {
                if (queueList.Count > 0)
                {
                    var t = queueList[0];
                    queueList.RemoveAt(0);
                    return t;
                }
            }

            return default(T);
        }
        /// <summary>
        /// 获得队列下一对象
        /// </summary>
        /// <returns></returns>
        public T MoveNext()
        {
            lock (lockObj)
            {
                if (Count == 0)
                    return default(T);

                if (index++ >= Count - 1)
                {
                    index = 0;
                }

                Current = queueList[index];
                return Current;
            }

        }

    }
}
